<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

  <script src="jquery.js"></script>

</head>
<body>
  <div>
    <form action="/addMedicalAssay" method="post" id="form">
      <table border="1px">
        <tr aria-colspan="2">
          <h2>添加核酸检测信息</h2>
        </tr>
        <tr>
          <td>被检测人姓名(*)</td>
          <td>
            <input type="text" name="assayUser">
          </td>
        </tr>
        <tr>
          <td>被检测人手机号(*)</td>
          <td> <input type="text" name="phone"></td>

        </tr>
        <tr>
          <td>被检测人身份证(*)</td>
          <td> <input type="text" name="cardNum"></td>

        </tr>
        <tr>
          <td>检测机构(*)</td>
          <td>
            <select name="hospitalId">
              <option value="0" selected>——请选择——</option>
              <option th:each="name : ${hospitalName}" th:text="${name.name}" th:value="${name.id}"></option>
            </select>
          </td>
        </tr>
        <tr>
          <td>检测日期(*)</td>
          <td>
            <input type="text" id="timeline" name="time">
          </td>
        </tr>
        <tr aria-colspan="2">
          <td>
            <input type="submit" value="添加">
          </td>
          <td>
            <button>返回</button>
          </td>
        </tr>
      </table>
    </form>
  </div>
</body>

<script>

   $("#form").submit(function (){

     var input = $("input[type=text]");
     for (var i = 0; i < input.length; i++) {

       if(input[i].value === ''){
         alert("请填写完整信息");
         return false;
       }
     }
     debugger
     var time = $("input[name='timeline']").val();
     alert(time)
     var indexOf = time.indexOf("-");
     var lastIndexOf = time.lastIndexOf("-");
     if(indexOf !== 4 && lastIndexOf !== 7){
       alert("日期格式不正确！")
       return false;
     }
     return true;
   })
</script>

</html>